package com.epalmpay.job;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.service.yunstream.IOilcardOrderService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Logger;
@Component
public class FlowAndOilChargeStatisJob {

	@Resource
	private ISysLogService sysLogService;
	@Resource
	private IOilcardOrderService oilcardOrderService;
	
	protected Logger logger = Logger.getLogger(this.getClass());	
	
	/**
	 * 交易日统计(在分润统计之后)
	 * 每天凌晨5点执行一次
	 */
	//@Scheduled(cron="0 0/1 * * * ?")
	
	@Scheduled(cron="0 0 5 * * ?")
	public void  run(){

		//统计日期
		Date statisDate = null;
		StringBuffer sb = new StringBuffer(); 
		try {			
			//取得昨天日期
			statisDate = DateUtil.getAfterDay(-1);

			oilcardOrderService.saveFlowChargeDayStatis(statisDate);
			logger.info(sb.append("计划任务:")
					.append(TaskType.flowChargeDayStatis.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
			try{
				sysLogService.recordSysTaskLog(TaskType.flowChargeDayStatis.getType(),
						statisDate, RunStatus.Success.getType());
			 }catch(Exception sube){
					logger.info(sb.append("计划任务:")
							.append(TaskType.flowChargeDayStatis.getName())
							.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));				 
				 logger.error(sube);
			 }
		} catch (ApplicationException ae) { 
			logger.info(sb.append("计划任务:")
					.append(TaskType.flowChargeDayStatis.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行结果：").append(ae.errMsg));				
		}		
		 catch (Exception e) { 
			 try{
				sysLogService.recordSysTaskLog(TaskType.flowChargeDayStatis.getType(),
						statisDate, RunStatus.Fail.getType());
			 }catch(Exception sube){
					logger.info(sb.append("计划任务:")
							.append(TaskType.flowChargeDayStatis.getName())
							.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));				 
				 logger.error(sube);
			 }
			e.printStackTrace();
			logger.info(sb.append("计划任务:")
					.append(TaskType.flowChargeDayStatis.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));			
			logger.error(e);
		}
		
		sb.setLength(0);
		
		
		try {			
			//取得昨天日期
			statisDate = DateUtil.getAfterDay(-1);

			oilcardOrderService.saveOilChargeDayStatis(statisDate);
			logger.info(sb.append("计划任务:")
					.append(TaskType.oilChargeDayStatis.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
			try{
				sysLogService.recordSysTaskLog(TaskType.oilChargeDayStatis.getType(),
						statisDate, RunStatus.Success.getType());
			 }catch(Exception sube){
					logger.info(sb.append("计划任务:")
							.append(TaskType.oilChargeDayStatis.getName())
							.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));				 
				 logger.error(sube);
			 }
		} catch (ApplicationException ae) { 
			logger.info(sb.append("计划任务:")
					.append(TaskType.oilChargeDayStatis.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行结果：").append(ae.errMsg));				
		}		
		 catch (Exception e) { 
			 try{
				sysLogService.recordSysTaskLog(TaskType.oilChargeDayStatis.getType(),
						statisDate, RunStatus.Fail.getType());
			 }catch(Exception sube){
					logger.info(sb.append("计划任务:")
							.append(TaskType.oilChargeDayStatis.getName())
							.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));				 
				 logger.error(sube);
			 }
			e.printStackTrace();
			logger.info(sb.append("计划任务:")
					.append(TaskType.oilChargeDayStatis.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));			
			logger.error(e);
		}
	}
	
	/**public void  run(){
		int days=150;
		for(int i=14;i<days;i++){
			Date statisDate = DateUtil.getAfterDay(-1*i);
			try {
				oilcardOrderService.saveFlowChargeDayStatis(statisDate);
				StringBuffer sb = new StringBuffer(); 
				logger.info(sb.append("计划任务:")
						.append(TaskType.oilChargeDayStatis.getName())
						.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				oilcardOrderService.saveOilChargeDayStatis(statisDate);
				StringBuffer sb = new StringBuffer(); 
				logger.info(sb.append("计划任务:")
						.append(TaskType.oilChargeDayStatis.getName())
						.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}**/

}
